package com.cjm.algorithm.leetcode;

import com.cjm.algorithm.TreeNode;

import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;

public class IsEvenOddTree1609 {
    public boolean isEvenOddTree(TreeNode root) {
        if (root == null)return false;
        if (root.val % 2 == 0) return false;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        int curlen = 1;
        int ce = 0;
        int nextlen = 0;
        while (!queue.isEmpty()) {
            boolean flag = true;
            if (ce % 2 != 0) {
                flag = false;
            }
            Integer pre = null;
            for (int i = 0; i < curlen; i++) {
                TreeNode poll = queue.poll();
                if (pre != null) {
                    if (flag && poll.val <= pre) {
                        return false;
                    }
                    if (!flag && poll.val >= pre) {
                        return false;
                    }
                }
                pre = poll.val;
                if (poll.left != null) {
                    TreeNode left = poll.left;
                    if (flag && left.val % 2 !=0) {
                        return false;
                    }
                    if (!flag && left.val % 2 == 0) {
                        return false;
                    }
                    queue.add(left);
                    nextlen++;
                }
                if (poll.right != null) {
                    TreeNode right = poll.right;
                    if (flag && right.val % 2 ==0) {
                        return false;
                    }
                    if (!flag && right.val % 2 != 0) {
                        return false;
                    }
                    queue.add(right);
                    nextlen++;
                }
            }
            ce++;
            curlen = nextlen;
            nextlen = 0;
        }
        return true;
    }

    public static void main(String[] args) {
        Queue<String> q = new ArrayBlockingQueue<String>(10);
        q.add("A");
        System.err.println(q.peek());
        System.err.println(q.peek());
        System.err.println(q.peek());
    }
}
